<?php
namespace Home\Controller;

class ProductController extends \Home\Controller\BaseController
{

	//获取商品信息
	public function goodlist(){
		$param = I('params');
		$type = $param['type'];
		$area = $param['area'];
		$page = $param['page'];
		$size = $param['size'];

		$map['_string'] = ' area = "'.$area.'" OR area = "请选择城市"';
		if($type){
			$map['type'] = $type;
		}

		$map['status'] = '1';
		$res = M("Good")
			->where($map)
			->field(array("id",'title','sub_title','postage','status','create_time','type','purchase_num','img_path','production_time','area','original_price','discount_price','stock','appointment',"end_time","id","supplier","appointment_node"))
			->order("create_time desc")
			->page($page, $size)
			->select();
		$return = array();
		foreach($res as $key=>$val){
			if($val['end_time']){
				$val['end_time'] = strtotime($val['end_time']);
			}else{
				unset($val['end_time']);
			}
			$return[$key] = $val;
			$return[$key]['original_price'] = floatval($val['original_price']);
			$return[$key]['discount_price'] = floatval($val['discount_price']);
			$return[$key]['create_time'] = strtotime($val['create_time']);
			$return[$key]['stock'] = intval($val['stock']);
			$img_path = explode(",",$val['img_path']);
			$val['img_path'] = array();
			if(is_array($img_path)){
				foreach($img_path as $ks=>$vs){
					if($vs == ""){
						continue;
					}
					$val['img_path'][] = "http://114.215.209.229".$vs;
				}
			}
			$return[$key]['img_path'] = $val['img_path'][0];
		}
		$this->ajaxReturn(array('error' => 0, 'info' => $return));
	}
	//获取商品详情
	public function goodinfo(){
		$param = I("params");
		$id = $param['id'];
		//类别
		//商品的详情
		$return['goodinfo'] = M("Good")->where(array("id"=>$id))->find();

		$return['goodinfo']['stock'] = intval($return['goodinfo']['stock']);
		$return['goodinfo']['stock'] = $return['goodinfo']['postage']?intval($return['goodinfo']['postage']):0;

//		unset($return['goodinfo']['info']);
//		$appcallbackurl = "http://114.215.209.229/Public/static/App/goodinfo".$id.".html";
//		if(file_exists("./Public/static/App/goodinfo".$id.".html")){
//			$return['goodinfo']['info'] = $appcallbackurl;
//		}
		$return['goodinfo']['min_price'] = floatval($return['goodinfo']['min_price']);
		$img_path = explode(",",$return['goodinfo']['img_path']);
		$return['goodinfo']['img_path'] = array();
		if(is_array($img_path)){
			foreach($img_path as $key=>$val){
				if($val == ""){
					continue;
				}
				$return['goodinfo']['img_path'][] = "http://114.215.209.229".$val;
			}
		}
		$return['goodinfo']["original_price"] = floatval($return['goodinfo']['original_price']);
		$return['goodinfo']["discount_price"] = floatval($return['goodinfo']['discount_price']);
		$return['goodinfo']['total_count'] = floatval($return['goodinfo']['total_count']);
		//选择最新更新的四款产品
		$return['recommendgoods'] = $this->recommendgoods();
		//判断是否团购商品
		$groupgoods = M("GroupGoods")->where(array("goods_id"=>$id))->find();
		if(count($groupgoods)){
			$ActiveGroup = M("ActiveGroup")->where(array("id"=>$groupgoods['group_id']))->find();
			$return['goodinfo']['clusters'] = $ActiveGroup['clusters'];
			$return['goodinfo']['type'] = '2';
			$return['goodinfo']['privilege'] = $ActiveGroup['privilege']>0?"团长开团".$ActiveGroup['privilege']."折":"无优惠";
			$return['goodinfo']['privilege_discount'] = $ActiveGroup['privilege']>0?$ActiveGroup['privilege']:0;
			$where = array();
			$where['goods_id']=$id;
			$where['end_time'] = array("gt",date("Y-m-d H:i:s"));
			$GroupOrder = M("GroupOrder")->where($where)->select();
			if(is_array($GroupOrder)){
				foreach($GroupOrder as $key=>$val){
					$diffclusters = $val['clusters']-$val['join_count'];
					if($diffclusters == 0){
						continue;
					}
					$end_time = strtotime($val['end_time'])-time();
					$return['groupOrder'][$key]['title'] = "还差".$diffclusters."人成团";
					$return['groupOrder'][$key]['end_time'] = cutTime($end_time);
					$return['groupOrder'][$key]['groupOrderId'] =$val['id'];
					$return['groupOrder'][$key]['avatar_url'] = "http://114.215.209.229/Uploads/Download/2016-08-21/57b97059e1529.jpg";
				}
			}
		}
		//判断是否团购商品
		$activegoods = M("ActiveGoods")->where(array("goods_id"=>$id))->find();
		if(count($activegoods)){
			$bargainorder = M("BargainOrder")->where(array("goods_id"=>$id))->select();
			$bargained = 0;
			foreach($bargainorder as $key=>$val){
				$bargained += $val['bargained'];
			}
			$return['goodinfo']['bargained'] = $bargained;//已经砍价多少
			$Active = M("Active")->where(array("id"=>$groupgoods['group_id']))->find();
			$return['goodinfo']['bargain_cycle'] = cutTime($Active['bargain_cycle']*60);
			$return['goodinfo']['type'] = '3';
		}
		//复购商品
		if($return['goodinfo']['re_purchase'] == '1' && $param['userToken'] && $return['goodinfo']['end_time']>date("Y-m-d H:i:s")){
			$userToken = M("UserToken")->where(array("user_token"=>$param['userToken']))->find();
			$userId = $userToken['user_id'];
			$investinfo = M("Invest")
				->join("left join mt_invest_goods on mt_invest.id=mt_invest_goods.invest_id")
				->where(array("user_id"=>$userId,"status"=>'1'))->select();
			if(count($investinfo)){
				$investgood = array();
				foreach($investinfo as $key=>$val){
					$investgood[$val['goods_id']]++;
				}
			}
			$goodinfo = $return['goodinfo'];
			$discount_price = floatval($goodinfo['discount_price']);
			$discount_price_tow = $goodinfo['discount_price_tow'];
			$discount_price_three = $goodinfo['discount_price_three'];
			$discount_price_four = $goodinfo['discount_price_four'];
			$discount_price_five = $goodinfo['discount_price_five'];
			if($investgood[$val['id']] == 1){
				$discount_price = $discount_price*$discount_price_tow/100;
				$return['goodinfo']['discounted'] = round($discount_price_tow/10,2);
			}elseif($investgood[$val['id']] == 2){
				$discount_price = $discount_price*$discount_price_three/100;
				$return['goodinfo']['discounted'] = round($discount_price_three/10,2);
			}elseif($investgood[$val['id']] == 3){
				$discount_price = $discount_price*$discount_price_four/100;
				$return['goodinfo']['discounted'] = round($discount_price_four/10,2);
			}elseif($investgood[$val['id']]>3){
				$discount_price = $discount_price*$discount_price_five/100;
				$return['goodinfo']['discounted'] = round($discount_price_five/10,2);
			}
			$return['goodinfo']['discount_price'] = $discount_price;
			$return['goodinfo']['type'] = '4';
			$return['goodinfo']['discount_time'] = $goodinfo['create_time']."~".$goodinfo['end_time'];
		}
		$userGoods = M("UserGoods")->where(array("good_id"=>$id))->find();
		if(count($userGoods)){
			$return['goodinfo']['isfollow'] = '1';
		}
		$this->ajaxReturn(array('error' => 0, 'info' => $return));
	}
	//选择最新更新的四款产品
	public function recommendgoods(){
		$map['status'] = '1';
		$groupgoods = M("Good")->where($map)->order("create_time desc")->limit(6)->select();
		$return = array();
		foreach($groupgoods as $key=>$val){
			$return[$key]['original_price'] = floatval($val['original_price']);
			$return[$key]['discount_price'] = floatval($val['discount_price']);
			$return[$key]['create_time'] = strtotime($val['create_time']);
			$return[$key]['id'] = $val['id'];
			$return[$key]['title'] = $val['title'];
			$return[$key]['type'] = $val['type'];
			$return[$key]['sub_title'] = $val['sub_title'];
			$return[$key]['stock'] = intval($val['stock']);
			$img_path = explode(",",$val['img_path']);
			$val['img_path'] = array();
			if(is_array($img_path)){
				foreach($img_path as $ks=>$vs){
					if($vs == ""){
						continue;
					}
					$val['img_path'][] = "http://114.215.209.229".$vs;
				}
			}
			$return[$key]['img_path'] = $val['img_path'][0];
		}
		return $return;
	}
	//拼团
	public function indexGroupGoods(){
		$param = I("params");
		$page = $param['page']?$param['page']:"1";
		$size = $param['size']?$param['size']:"2";
		$map['mt_good.status'] = '1';
		$groupgoods = M("GroupGoods")
			->join("left join mt_good on mt_group_goods.goods_id=mt_good.id")
			->where($map)
			->page($page, $size)
			->select();
		$return = array();
		foreach($groupgoods as $key=>$val){
			$return[$key]['original_price'] = floatval($val['original_price']);
			$return[$key]['discount_price'] = floatval($val['discount_price']);
			$return[$key]['create_time'] = strtotime($val['create_time']);
			$return[$key]['id'] = $val['goods_id'];
			$return[$key]['title'] = $val['title'];
			$return[$key]['type'] = '2';
			$return[$key]['sub_title'] = $val['sub_title'];
			$return[$key]['stock'] = intval($val['stock']);
			$img_path = explode(",",$val['img_path']);
			$val['img_path'] = array();
			if(is_array($img_path)){
				foreach($img_path as $ks=>$vs){
					if($vs == ""){
						continue;
					}
					$val['img_path'][] = "http://114.215.209.229".$vs;
				}
			}
			$return[$key]['img_path'] = $val['img_path'][0];
		}
		$this->ajaxReturn(array('error' => 0, 'info' => $return));
	}
	//砍价
	public function indexActiveGoods(){
		$param = I("params");
		$page = $param['page']?$param['page']:"1";
		$size = $param['size']?$param['size']:"4";
		$map['mt_good.status'] = '1';
		$groupgoods = M("ActiveGoods")->join("left join mt_good on mt_active_goods.goods_id=mt_good.id")->where($map)->page($page, $size)->select();
		$return = array();
		foreach($groupgoods as $key=>$val){
			$return[$key]['id'] = $val['id'];
			$return[$key]['original_price'] = floatval($val['original_price']);
			$return[$key]['discount_price'] = floatval($val['discount_price']);
			$return[$key]['bargained'] = "10";
			$return[$key]['create_time'] = strtotime($val['create_time']);
			$return[$key]['title'] = $val['title'];
			$return[$key]['type'] = '3';
			$return[$key]['sub_title'] = $val['sub_title'];
			$return[$key]['stock'] = intval($val['stock']);
			$img_path = explode(",",$val['img_path']);
			$val['img_path'] = array();
			if(is_array($img_path)){
				foreach($img_path as $ks=>$vs){
					if($vs == ""){
						continue;
					}
					$val['img_path'][] = "http://114.215.209.229".$vs;
				}
			}
			$return[$key]['img_path'] = $val['img_path'][0];
		}
		$this->ajaxReturn(array('error' => 0, 'info' => $return));
	}

	//捡便宜
	public function getmoreGoods(){
		//获取用户的订单详情
		$userId = $this->user['userId'];
		$investinfo = M("Invest")
			->join("left join mt_invest_goods on mt_invest.id=mt_invest_goods.invest_id")
			->where(array("user_id"=>$userId,"status"=>'1'))->select();
		if(count($investinfo)){
			$investgood = array();
			foreach($investinfo as $key=>$val){
				$investgood[$val['goods_id']]++;
			}
		}
		$param = I("params");
		$page = $param['page'];
		$size = $param['size'];
		$map['status'] = '1';
		$map['re_purchase'] = '1';
		$groupgoods = M("Good")
			->where($map)->page($page, $size)->select();
		$return = array();
		foreach($groupgoods as $key=>$val){
			$discount_price = floatval($val['discount_price']);
			$discount_price_tow = $val['discount_price_tow'];
			$discount_price_three = $val['discount_price_three'];
			$discount_price_four = $val['discount_price_four'];
			$discount_price_five = $val['discount_price_five'];
			$return[$key]['discounted'] = 10;
			if($investgood[$val['id']] == 1){
				$discount_price = $discount_price*$discount_price_tow/100;
				$return[$key]['discounted'] = round($discount_price_tow/10,2);
			}elseif($investgood[$val['id']] == 2){
				$discount_price = $discount_price*$discount_price_three/100;
				$return[$key]['discounted'] = round($discount_price_three/10,2);
			}elseif($investgood[$val['id']] == 3){
				$discount_price = $discount_price*$discount_price_four/100;
				$return[$key]['discounted'] = round($discount_price_four/10,2);
			}elseif($investgood[$val['id']]>3){
				$discount_price = $discount_price*$discount_price_five/100;
				$return[$key]['discounted'] = round($discount_price_five/10,2);
			}
			$return[$key]['type'] = '4';
			$return[$key]['id'] = $val['id'];
			$return[$key]['original_price'] = floatval($val['original_price']);
			$return[$key]['discount_price'] = floatval($discount_price);
			$return[$key]['create_time'] = strtotime($val['create_time']);
			$return[$key]['title'] = $val['title'];
			$return[$key]['sub_title'] = $val['sub_title'];
			$return[$key]['stock'] = intval($val['stock']);
			$img_path = explode(",",$val['img_path']);
			$val['img_path'] = array();
			if(is_array($img_path)){
				foreach($img_path as $ks=>$vs){
					if($vs == ""){
						continue;
					}
					$val['img_path'][] = "http://114.215.209.229".$vs;
				}
			}
			$return[$key]['img_path'] = $val['img_path'][0];
		}
		$this->ajaxReturn(array('error' => 0, 'info' => $return));
	}
	//关键字搜索
	public function searchbykey(){
		$param = I("params");
		$user_id = $this->user['userId'];
		$key_name = $param['key'];
		$searchtype = $param['search_type'];
		$sort = $param['sort'];
		$order = "";
		if($searchtype!=1){
			if($searchtype == 2){
				$goodinvest = array();
				$investlist = M("Invest")->where(array("status"=>"1"))->select();
				if(is_array($investlist)){
					foreach($investlist as $key=>$val){
						$good_info = json_decode($val['good_info'],true);
						if(!is_array($good_info)){
							$good_info = json_decode($good_info,true);
						}
						foreach($good_info as $k=>$v){
							$goodinvest[$v["good_id"]] += $v['count'];
						}
					}
				}
				if($sort == 1){
					asort($goodinvest);
				}elseif($sort == 2){
					arsort($goodinvest);
				}
				function sortArrByField(&$array, $field, $desc = false){
					$fieldArr = array();
					foreach ($array as $k => $v) {
						$fieldArr[$k] = $v[$field];
					}
					$sort = $desc == false ? SORT_ASC : SORT_DESC;
					array_multisort($fieldArr, $sort, $array);
				}
			}
			switch($searchtype){
				case 3://价格
					if($sort == 1){
						$order = "discount_price asc";
					}elseif($sort == 2){
						$order = "discount_price desc";
					}
					break;
				case 4://最新
					if($sort == 1){
						$order = "create_time asc";
					}elseif($sort == 2){
						$order = "create_time desc";
					}
					break;
			}
		}
		$where['title'] = array("like","%".$key_name."%");
		$where['status'] ='1';
		$where['stock'] = array("gt",0);
		$Good = M("Good");
		if($order){
			$Good->order($order);
		}
		$goodinfo = $Good->where($where)->field(
			array("id","title",
				"sub_title",
				"status",
				"create_time",
				"type",
				"purchase_num",
				"img_path",
				"production_time",
				"area",
				"min_price",
				"appointment",
				"stock",
				"original_price",
				"discount_price",
				"end_time",
				"province",
				"city",
				"freeprovince",
				"postage",
				"supplier"))->select();


		$return = array();
		if(is_array($goodinfo)){
			foreach($goodinfo as $key=>$val){
				$img_path = explode(",",$val['img_path']);
				$val['img_path'] = "http://114.215.209.229".$img_path[0];
				$val['original_price'] = floatval($val['original_price']);
				$val['discount_price'] = floatval($val['discount_price']);
				$val['min_price'] = floatval($val['min_price']);
				$val['stock'] = intval($val['stock']);
				if($val['end_time']){
					$val['end_time'] = strtotime($val['end_time']);
				}else{
					unset($val['end_time']);
				}
				if(count($goodinvest[$val['id']])){
					$val['count'] = $goodinvest[$val['id']];
				}else{
					$val['count'] = "0";
				}
				$return[$key] = $val;
			}
			if($searchtype == 2){
				if($sort == 1){
					sortArrByField($return,"count",true);
				}else{
					sortArrByField($return,"count",false);
				}
			}

		}
		$this->ajaxReturn(array('error' => 0, 'info' => $return));
	}
	//关键字搜索
	public function keylist(){
		$user_id = $this->user['userId'];
		$hotkey = global_config('hotkey');
		$this->ajaxReturn(array('error' => 0, 'info' => explode(",",$hotkey)));
	}

}
